home *** CD-ROM | disk | FTP | other *** search
/ ADA Programming Guide / ADA Programming Guide.iso / ada_gwu / bmain.c < prev    next >
C/C++ Source or Header  |  1996-01-30  |  8KB  |  340 lines

  1. /*
  2.  * Copyright (C) 1985-1992  New York University
  3.  * 
  4.  * This file is part of the Ada/Ed-C system.  See the Ada/Ed README file for
  5.  * warranty (none) and distribution info and also the GNU General Public
  6.  * License for more details.
  7.  
  8.  */
  9.  
  10. #define GEN
  11.  
  12. #include <stdio.h>
  13. #include <ctype.h>
  14. #include "hdr.h"
  15. #include "vars.h"
  16. #include "gvars.h"
  17. #include "libhdr.h"
  18. #include "segment.h"
  19. #include "ifile.h"
  20. #include "slot.h"
  21. #include "arithp.h"
  22. #include "dclmapp.h"
  23. #include "readp.h"
  24. #include "dbxp.h"
  25. #include "initp.h"
  26. #include "blibp.h"
  27. #include "libp.h"
  28. #include "glibp.h"
  29. #include "libfp.h"
  30. #include "librp.h"
  31. #include "libwp.h"
  32. #include "g0ap.h"
  33. #include "setp.h"
  34. #include "miscp.h"
  35. #include "gmiscp.h"
  36. #include "bmainp.h"
  37.  
  38. static void fold_upper(char *);
  39. static void bpreface();
  40. static void exitf(int);
  41.  
  42. /* Driver routine for ada gen */
  43. char *argname;
  44.  
  45. IFILE    *AISFILE, *AXQFILE, *STUBFILE, *LIBFILE, *TREFILE;
  46. int list_unit_0 = 0; /* set by '0' option to list unit 0 structure */
  47. int peep_option = 1; /* on for peep_hole optimization */
  48. int adacomp_option = 0; /* set if called from adacomp */
  49.  
  50. extern Segment    CODE_SEGMENT, DATA_SEGMENT, DATA_SEGMENT_MAIN;
  51. extern Segment   VARIANT_TABLE, FIELD_TABLE ;
  52. char *lib_name;
  53. #ifdef DEBUG
  54. extern int zpadr_opt; /* not for EXPORT */
  55. #endif
  56.  
  57. void main(int argc, char **argv)
  58. {
  59.     int        c, i, n;
  60.     int        errflg = 0, nobuffer = 0, mflag = 0;
  61.         int    lib_opt = FALSE;
  62.     extern int  optind;
  63.     extern char *optarg;
  64.     char    *t_name;
  65.     char    *fname, *tfname, *source_name;
  66.  
  67.     AISFILE = (IFILE *)0;
  68.     AXQFILE = (IFILE *)0;
  69.     LIBFILE = (IFILE *)0;
  70.     STUBFILE = (IFILE *)0;
  71.     TREFILE = (IFILE *)0;
  72.  
  73.     MAINunit = "";
  74.     interface_files = "";
  75.  
  76. #ifdef IBM_PC
  77.     while ((c = getopt (argc, argv, "C:c:G:g:M:m:i:l:L:")) != EOF) {
  78. #else
  79.     while ((c = getopt (argc, argv, "c:g:m:i:l:")) != EOF) {
  80. #endif
  81.         /*
  82.          *    user:
  83.          *    c    set if called from adacomp (errors in msg format).
  84.          *    g    debugging, followed by list of options:
  85.          *        0    show structure of unit 0
  86.          *        b    do not buffer standard output
  87.          *        e    flag signalling errors in the parsing phase
  88.          *        g    list generated code
  89.          *        l    show line numbers in generated code
  90.          *        z    call trapini to initialize traps
  91.          *      i   to specify object files and libraries for pragma interface
  92.          *      l    using library
  93.          *    m    main unit name
  94.          */
  95. #ifdef IBM_PC
  96.         if (isupper(c)) c = tolower(c);
  97. #endif
  98.         switch (c) {
  99.         case 'c': 
  100.             adacomp_option++;
  101.             source_name = malloc(strlen(optarg)+1);
  102.             strcpy(source_name, optarg);
  103.             break;
  104.         case 'i':
  105.             interface_files = strjoin(interface_files, optarg);
  106.             interface_files = strjoin(interface_files, " ");
  107.             break;
  108.         case 'l': /* using existing library */
  109.                         lib_opt = TRUE;
  110.                         lib_name = emalloc(strlen(optarg) + 1);
  111.             strcpy(lib_name, optarg);
  112.             break;
  113.         case 'm': /* specify main unit name */
  114.             MAINunit = malloc(strlen(optarg)+1);
  115.             strcpy(MAINunit, optarg);
  116.             fold_upper(MAINunit);
  117.             break;
  118.         case 'g': /* gen debug options */
  119.             n = strlen(optarg);
  120.             for (i = 0; i < n; i++) {
  121. #ifdef IBM_PC
  122.                         fold_lower(optarg);
  123. #endif
  124.                 switch((int)optarg[i]) {
  125. #ifdef DEBUG
  126.                 case 'a':
  127.                     zpadr_opt = 0; /* do not print addresses in zpadr */
  128.                     break;
  129. #endif
  130.                 case 'g':
  131.                     list_code++;
  132.                     break;
  133.                 case 'l':
  134.                     line_option++;
  135.                     break;
  136. #ifdef DEBUG
  137.                 case 'b': /* do not buffer output */
  138.                     nobuffer++;
  139.                     break;
  140.                 case 'd': /* force debugging output */
  141.                     debug_flag++;
  142.                     break;
  143.                 case 'e':
  144.                     errors = TRUE;
  145.                     break;
  146.                 case 'o': /* disable optimization (peep) */
  147.                     peep_option = 0;
  148.                     break;
  149.                 case '0': /* read trace including unit 0 */
  150.                     list_unit_0++;
  151.                     break;
  152.                 case 'z': 
  153.                     trapini();
  154.                     break;
  155. #endif
  156.                 }
  157.             }
  158.             break;
  159.         case '?':
  160.             errflg++;
  161.         }
  162.         }
  163. #ifdef IBM_PC
  164.     if (!adacomp_option) {
  165.         fprintf(stderr, "NYU Binder Version 1.11.1,");
  166.         fprintf(stderr, " Copyright (C) 1985-1992 by New York University\n");
  167.     }
  168. #endif
  169.     fname = (char *)0;
  170.     if (optind < argc)
  171.         fname = argv[optind];
  172.     /* if fname not given, get from environment. */
  173.     if (!errflg && !lib_opt && fname == (char *)0) {
  174.         fname = getenv("ADALIB");
  175.         if (fname!= (char *)0 && !adacomp_option) {
  176. #ifdef IBM_PC
  177.             fprintf(stderr, "L");
  178. #else
  179.             fprintf(stderr, "l");
  180. #endif
  181.             fprintf(stderr, "ibrary defined by ADALIB: %s\n", fname);
  182.         }
  183.     }
  184.     if ((!lib_opt && fname == (char *)0) || errflg) {
  185.            fprintf (stderr, "Usage: adabind [-m main_unit] [-l library]\n");
  186.            exitp(RC_ABORT);
  187.     }
  188.     if (!lib_opt) {
  189.            lib_name = emalloc(strlen(fname) + 1);
  190.        strcpy(lib_name, fname);
  191.         }
  192.     t_name = libset(lib_name); /* set library */
  193.     gen_option = FALSE; /* bind only */
  194.  
  195.     tup_init(); /* initialize set and tuple procedures */
  196.     FILENAME = (fname != (char *)0) ? strjoin(fname, "") : lib_name;
  197.  
  198.     PREDEFNAME = predef_env();
  199.     if (nobuffer) {
  200.         setbuf (stdout, (char *) 0);    /* do not buffer output (for debug) */
  201.     }
  202.     rat_init(); /* initialize arithmetic and rational package*/
  203.     dstrings_init(2048, 256); /* initialize dstrings package */
  204.     init_sem();
  205.     DATA_SEGMENT_MAIN = main_data_segment();
  206.     aisunits_read = tup_new(0);
  207.     init_symbols = tup_exp(init_symbols, seq_symbol_n);
  208.     for (i = 1; i <= seq_symbol_n; i++)
  209.         init_symbols[i] = seq_symbol[i];
  210.  
  211.     num_predef_units = init_predef();
  212.     /*
  213.      * When the separate compilation facility is being used all references to
  214.      * AIS files will be made via the directory in LIBFILE. AISFILENAME is set
  215.      * to a number.
  216.      */
  217.     if (new_library) 
  218.         AISFILENAME = "1";
  219.     else
  220.         AISFILENAME = lib_aisname(); /* retrieve name from library */
  221.     /* open the appropriate files using the suffix .axq for axq files and
  222.      * .trc for tree file. 
  223.      *
  224.      * Open MESSAGEFILE with suffixe ".msg" if a file name specified;
  225.      * otherwise, if a file name not required, and one is not given,
  226.      * used stderr.
  227.      */
  228.     AXQFILE  = ifopen(AISFILENAME, "axq", "w", 0);
  229.     if (adacomp_option) {
  230.         MSGFILE  = efopenl(source_name, "msg", "a", "t");
  231.         /* unbuffer output for debugging purposes */
  232.         setbuf(MSGFILE, (char *) 0);
  233.     }
  234.     else {
  235.         MSGFILE = stdout;
  236.     }
  237.     bpreface();
  238.  
  239.     /* Code formerly procedure finit() in init.c is now put here directly */
  240.     if (!errors) {
  241.         write_glib();
  242.         cleanup_files();
  243.     }
  244.  
  245.     exitf(RC_SUCCESS);
  246. }
  247.  
  248. static void fold_upper(char *s)                                /*;fold_upper*/
  249. {
  250.     register char c;
  251.  
  252.     while (c = *s) {
  253.         if (islower(c)) *s = toupper(c);
  254.         s++;
  255.     }
  256. }
  257.  
  258. void fold_lower(char *s)                                        /*;fold_lower*/
  259. {
  260.     register char c;
  261.  
  262.     while (c = *s) {
  263.         if (isupper(c)) *s = tolower(c);
  264.         s++;
  265.     }
  266. }
  267.  
  268. static void bpreface()                                            /*;bpreface*/
  269. {
  270.     /* bpreface is version of preface for use with binder */
  271.  
  272.     int    i;
  273.     Tuple    aisread_tup;
  274.  
  275.     aisread_tup = tup_new(0);
  276.     initialize_1();
  277.     /* 1- Load PREDEF */
  278.  
  279.     TASKS_DECLARED = FALSE;
  280.     /* 2- Generate user program */
  281.  
  282.     initialize_2();
  283.  
  284.     ada_line = 9998;
  285.     /* if binding, make ais_read tupe correspond to library */
  286.     aisread_tup = tup_new(0);
  287.     for (i = 11; i <= unit_numbers; i++)
  288.         aisread_tup = tup_with(aisread_tup, pUnits[i]->name);
  289.  
  290. #ifdef EXPORT
  291.     list_code = 0;
  292. #endif
  293.     if (binder(aisread_tup))
  294.         store_axq(AXQFILE, unit_number_now);
  295.     ifclose(AXQFILE);
  296.     if (errors) {
  297. #ifdef DEBUG
  298.         user_info("Binding stopped");
  299. #endif
  300.         exitf(RC_ERRORS);
  301.     }
  302. }
  303.  
  304. static void exitf(int status)                                        /*;exitf*/
  305. {
  306.     /* exit after closing any open files */
  307.     ifoclose(AXQFILE);
  308.     ifoclose(LIBFILE);
  309.     ifoclose(STUBFILE);
  310.     exitp(status);
  311. }
  312.  
  313. void user_error(char *reason)                                    /*;user_error*/
  314. {
  315.     errors++;
  316.     if (adacomp_option) {
  317.         list_hdr(ERR_SEMANTIC);
  318.         fprintf(MSGFILE, " %s\n", reason);
  319.     }
  320.     else
  321.         printf(" %s\n", reason);
  322. }
  323.  
  324. void user_info(char *line)                                        /*;user_info*/
  325. {
  326.     /* In SETL USER_INFO macro is defined to be
  327.      * PRINTA(GENfile, INFORMATION, ada_line, 0, ada_line, 0, '    '+line)    endm;
  328.      * where the argument is always a unit_name passed to formatted name
  329.      * In C, we call user_info and fill in needed info
  330.      */
  331.  
  332.     if (adacomp_option) {
  333.         list_hdr(INFORMATION);
  334.         fprintf(MSGFILE, "%s\n", line);
  335.     }
  336.     else {
  337.         printf("%s\n", line);
  338.     }
  339. }
  340.